import { defineStore } from "pinia";
import { ref } from "vue";
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';

export default defineStore("bigscreen", ()=>{
    const floorId =  ref()
    // 校区id
    const campusId = ref()
    const floorList = ref([])
    const classInfoList = ref([])
    const botEchartsData = ref([])
    const euqipmentCountList = ref([])
    const classStatus = ref({
         freeTruck: 0,
        workingTruck: 0,
        offlineTruck: 0,
        zixijiaoshi: 0
    })
    function getFloorList(id) {
        campusId.value = id
         EsAjax.post(ApiUrls.getAllBuilding,{campusId:id}).then(res => {
             if (res.data.code === 200) {
                 floorList.value = res.data.data
                //  floorId.value = res.data.data[0].id
             }
    })
  }
  const lonlat = ref([])
   function setLnglat(payload) { // 地图定位
       if ((payload instanceof Array)) {
                lonlat.value = payload
            } else {
                throw Error('请输入正确的经纬度')
            }
    }
    function getRoomDetail(floorId=0,campusId=0) {
         let params = {
        buildingId: floorId,
        campusId
    }
    EsAjax.post(ApiUrls.getClassInfo, params).then(res => {
        if (res.data.code === 200) {
            let newArr = []
            let roomObj = {
                 // 上课教室
            freeTruck:0,
            // 自习教室
            workingTruck: 0,
            // 空闲教室
            offlineTruck: 0,
            //故障教室
            zixijiaoshi:0
            }
            res.data.data.map(item => {
                if (item.buildingList) {
                    item.buildingList.map(v => {
                    let obj = {
                        sclArea: item.campusName,
                        sclFloor: v.buildingName,
                        roomNum: v.classroomCount,
                        used: v.inUseCount,
                        noUse: v.freeCount
                    }
                    newArr.push(obj)
                   roomObj.freeTruck+=v.inUseCount
                    roomObj.workingTruck+=v.freeCount
                    roomObj.offlineTruck+=v.classroomCount
                    roomObj.zixijiaoshi=0
                })
                } 
            })
            classInfoList.value = newArr
            classStatus.value = roomObj
        }
    })
    }
    // 获取设备
    function getEuqipment(floorId = 0, campusId = 0) {
     let params = {
        buildingId: floorId,
        campusId,
    }
    EsAjax.post(ApiUrls.getEuqipmentCountAndStatus,params).then(res => {
        if (res.data.code === 200) {
            euqipmentCountList.value = res.data.data
            console.log(euqipmentCountList.value);
        }
        
    })
    }
    function getBuildUseage(floorId = 0, campusId = 0) {
         let params = {
        buildingId: floorId,
        campusId,
        limit:30
    }
        EsAjax.post(ApiUrls.getBuildUseage, params).then(res => {
            if (res.data.code === 200) {
                botEchartsData.value = JSON.parse(JSON.stringify(res.data.data))
            }
        })
    }
   return {
    floorId,
       floorList,
     getFloorList,
     lonlat,
       setLnglat,
       campusId,
       getRoomDetail,
       classInfoList,
       classStatus,
       getEuqipment,
       getBuildUseage,
       botEchartsData,
       euqipmentCountList
  }
});
